<?php
//  This file is part of EkkinCMS.
//
//  EkkinCMS is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  EkkinCMS is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with EkkinCMS.  If not, see <http://www.gnu.org/licenses/>.

// This file load the requierd page for the specific content

include("includes/plugins.php");

function page_get_next_id(){
    global $db;
    $r = $db->query( "SELECT * FROM pages" );
    $ids = array();
    foreach($r as $row){
        $ids[] = $row[id];
    }
    return max($ids)+1;
}

function page_get($id){
    global $lang, $main_site_gtrans,$db;
    // get page code from db
    $r = $db->get_values_c( "pages", "id = $id AND lang = \"$lang\"" );
    $en = 0;
    if ( $r[content] == "" ) {
        // english or google translate?
        $r = $db->get_values_c( "pages", "id = $id AND (lang = \"en\" OR lang = \"en_US\")" );
        $en = 1;
    }

    $site_content = html_entity_decode($r[content], ENT_QUOTES);
    $site_title = html_entity_decode($r[title], ENT_QUOTES);

    if ($en == 1 and $main_site_gtrans == 'true'){
        $site_content = util_translate($site_content, "en", $lang);
        $site_title = util_translate($site_title, "en", $lang);

    }

    return array("content" => $site_content, "title" => $site_title);
}

// if no var is set, load page 0
if (!isset($_GET["page"])){
    $r = $db->get_values_c("config", "name = 'site_default'");
    $current_page = $r['value'];
}
else {
    $current_page = $_GET["page"];
}

$current_page = $current_page;

if ($admin && $edit){
    include("admin/edit-page.php");
}
elseif ($admin && $translate){
    include("admin/translate-page.php");
}
elseif ($admin && $managemenu){
    include("admin/manage-menu.php");
}
elseif ($admin && $remove){
    include("admin/remove-page.php");
}
else {

    $data = page_get($current_page);
    $site_title = $data[title];
    $site_content = $data[content];

    // render special handeling on site_content
    $site_content = execute_plugins($site_content);


}

?>
